package cn.fscode.code.generator.service.impl;

import cn.fscode.commons.core.page.PageDomain;
import cn.fscode.commons.core.page.TableSupport;
import cn.fscode.commons.tool.core.StringUtils;
import cn.fscode.commons.tool.core.exception.Assert;
import cn.fscode.code.generator.mapper.SysDictItemMapper;
import cn.fscode.code.generator.mapper.SysDictMapper;
import cn.fscode.code.generator.model.po.SysDictItemPO;
import cn.fscode.code.generator.model.po.SysDictPO;
import cn.fscode.code.generator.model.req.SysDictQueryReq;
import cn.fscode.code.generator.service.ISysDictService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shenguangyang
 */
@Service
public class SysDictServiceImpl implements ISysDictService {
    @Resource
    private SysDictMapper dictMapper;
    @Resource
    private SysDictItemMapper dictItemMapper;

    public LambdaQueryWrapper<SysDictPO> getQueryWrapper(SysDictQueryReq req) {
        Assert.notNull(req, "非法数据");

        return Wrappers.<SysDictPO>lambdaQuery()
                .like(StringUtils.isNotEmpty(req.getDictName()), SysDictPO::getDictName, req.getDictName())
                .eq(StringUtils.isNotEmpty(req.getStatus()), SysDictPO::getStatus, req.getStatus())
                .like(StringUtils.isNotEmpty(req.getDictCode()), SysDictPO::getDictCode, req.getDictCode())
                .ge(!Objects.isNull(req.getBeginTime()), SysDictPO::getCreateTime, req.getBeginTime())
                .le(!Objects.isNull(req.getEndTime()), SysDictPO::getCreateTime, req.getEndTime())
                .orderByDesc(SysDictPO::getCreateTime);
    }

    @Override
    public List<SysDictPO> list(SysDictQueryReq req) {
        return dictMapper.selectList(getQueryWrapper(req));
    }

    @Override
    public IPage<SysDictPO> pageDict(SysDictQueryReq dict) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        return dictMapper.selectPage(new Page<>(pageNum, pageSize), getQueryWrapper(dict));
    }

    @Override
    public List<SysDictPO> listAll() {
        return dictMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public Map<String, List<SysDictItemPO>> listAllDictItem() {
        Map<String, List<SysDictItemPO>> data = new HashMap<>();
        List<SysDictPO> sysDictPOList = dictMapper.selectList(new QueryWrapper<>());
        List<SysDictItemPO> sysDictItemPOList = dictItemMapper.selectList(new QueryWrapper<>());
        for (SysDictPO sysDictPO : sysDictPOList) {
            List<SysDictItemPO> dictItemList = sysDictItemPOList.stream().filter(item -> item.getDictId().equals(sysDictPO.getId())).collect(Collectors.toList());
            data.put(sysDictPO.getDictCode(), dictItemList);
        }
        return data;
    }

    @Override
    public SysDictPO getById(Long dictId) {
        return dictMapper.selectById(dictId);
    }

    @Override
    public SysDictPO getByCode(String dictCode) {
        LambdaQueryWrapper<SysDictPO> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysDictPO::getDictCode, dictCode);
        return dictMapper.selectOne(lqw);
    }

    @Override
    public void deleteBatchByIds(Long[] dictIds) {
        dictMapper.deleteBatchIds(Arrays.stream(dictIds).collect(Collectors.toList()));
    }

    @Override
    @Transactional
    public void deleteById(Long dictId) {
        dictMapper.deleteById(dictId);
    }

    @Override
    public void loadingDictCache() {

    }

    @Override
    public void clearDictCache() {

    }

    @Override
    public void resetDictCache() {

    }

    @Override
    public int saveDict(SysDictPO dictType) {
        return dictMapper.insert(dictType);
    }

    @Override
    public int updateDict(SysDictPO dict) {
        return dictMapper.updateById(dict);
    }
}
